package com.janko.springbootbase.designmode.singleton;

import lombok.SneakyThrows;

/**
 * @ClassName Singleton
 * @Description 单例模式
 * @Author janko
 * @Date 2020/12/11 10:18
 */
public class Singleton1EHan extends BaseSingleton {
    /**
     * 饿汉式(线程安全，调用效率高，但是不能延时加载)
     * 缺点：单例在还没有使用到的时候，初始化就已经完成了。  但是先初始化了
     */
    private static Singleton1EHan instance = new Singleton1EHan();

    private Singleton1EHan() {
    }

    public static Singleton1EHan getInstance() throws InterruptedException {
        Thread.sleep(10);
         return instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    System.out.println(System.identityHashCode(Singleton1EHan.getInstance()));
                }
            }).start();
        }
    }
}
